package com.persistence.dao.mysql;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.model.Modulacion;
import com.persistence.dao.DAOException;
import com.persistence.dao.DAOManagerMain;
import com.persistence.dao.interfaces.DAOModulacion;

public class MySQLDAOModulacion implements DAOModulacion {
	
	private Modulacion modulacion = null;
	
	
	public MySQLDAOModulacion() {
	}

	public int insert(Modulacion modulacion) throws DAOException {
		Connection connection = null;
		ResultSet rs = null;
		PreparedStatement stmt = null;

		try {
			connection = DAOManagerMain.manager().getConnection();
			stmt = connection.prepareStatement("insert into Modulacion ("
					+ "Id_Modulacion, Descripcion, Total "
					+ ") " + "values (?, ?, ?) ");

			stmt.setLong(1, modulacion.getNroModulacion());
			stmt.setString(2, modulacion.getNombre());
			stmt.setLong(3, modulacion.getTotal());
			stmt.executeUpdate();
			
			return 1;
		} catch (SQLException sqle) {
			throw new DAOException(sqle);

		} finally {
			DAOManagerMain.manager().closeAll(connection, stmt, rs);
		}
	}

	public int update(Modulacion modulacion) throws DAOException {
		Connection connection = null;
		ResultSet rs = null;
		PreparedStatement stmt = null;

		try {
			connection = DAOManagerMain.manager().getConnection();
			stmt = connection
					.prepareStatement("update Modulacion " 
							+ "set Nombre = ?, Total = ?"
							+ " where id_Modulacion = ? ");

		   stmt.setString(1, modulacion.getNombre());
         stmt.setLong(2, modulacion.getTotal());

			stmt.executeUpdate();
			return 1;
		} catch (SQLException sqle) {
			throw new DAOException(sqle);
		} finally {
			DAOManagerMain.manager().closeAll(connection, stmt, rs);
		}
	}

	public int eliminate(Modulacion modulacion) throws DAOException {
		Connection connection = null;
		ResultSet rs = null;
		PreparedStatement stmt = null;

		try {
			connection = DAOManagerMain.manager().getConnection();
			stmt = connection
					.prepareStatement("delete from Modulacion where id_Modulacion = ? ");
			stmt.setLong(1, modulacion.getNroModulacion());

			stmt.executeUpdate();
			return 1;
		} catch (SQLException sqle) {
			throw new DAOException(sqle);
		} finally {
			DAOManagerMain.manager().closeAll(connection, stmt, rs);
		}
	}

	public ArrayList<Modulacion> select() throws DAOException {
		Connection connection = null;
		ResultSet rs = null;
		PreparedStatement stmt = null;
		ArrayList<Modulacion> modulacions = new ArrayList<Modulacion>();

		try {
			connection = DAOManagerMain.manager().getConnection();
			stmt = connection.prepareStatement("select * from Modulacion");
			rs = stmt.executeQuery();

			while (rs.next()) {
				Modulacion modulacion = new Modulacion();
				modulacion.setNroModulacion(rs.getInt("Id_Modulacion"));
				modulacion.setNombre(rs.getString("descripcion"));
				modulacion.setTotal(rs.getInt("total"));

				modulacions.add(modulacion);
			}

			return modulacions;
		} catch (SQLException sqle) {
			throw new DAOException(sqle);
		} finally {
			DAOManagerMain.manager().closeAll(connection, stmt, rs);
		}
	}

	public Modulacion findById(int id) throws DAOException {
		Connection connection = null;
		ResultSet rs = null;
		PreparedStatement stmt = null;
		modulacion = new Modulacion();

		try {
			connection = DAOManagerMain.manager().getConnection();
			stmt = connection
					.prepareStatement("select * from Modulacion where Id_Modulacion = ?");
			stmt.setLong(1, id);
			rs = stmt.executeQuery();

			if (rs.next()) {
			   modulacion.setNroModulacion(rs.getInt("Id_Modulacion"));
            modulacion.setNombre(rs.getString("descripcion"));
            modulacion.setTotal(rs.getInt("total"));
			}

			return modulacion;
		} catch (SQLException sqle) {
			throw new DAOException(sqle);
		} finally {
			DAOManagerMain.manager().closeAll(connection, stmt, rs);
		}
	}

	@Override
	public boolean existInstance(Modulacion modulacion) throws DAOException {
		Connection connection = null;
		ResultSet rs = null;
		PreparedStatement stmt = null;
		boolean result = false;

		try {
			connection = DAOManagerMain.manager().getConnection();
			stmt = connection
					.prepareStatement("select * from Modulacion where UCASE(descripcion) = ?");

			stmt.setString(1, modulacion.getNombre().toUpperCase());
//			stmt.setString(2, modulacion.getApellido().toUpperCase());
			rs = stmt.executeQuery();

			if (rs.next()) {
				result = true;
			}

			return result;
		} catch (SQLException sqle) {
			throw new DAOException(sqle);
		} finally {
			DAOManagerMain.manager().closeAll(connection, stmt, rs);
		}
	}
}
